package work.comeback.omo.dao.movie.imp;


import work.comeback.omo.dao.movie.IMovieDetail;
import work.comeback.omo.model.movie.detail.MovieDetailData;
import work.comeback.omo.util.configuration.Application;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MovieDetailImpl implements IMovieDetail {
    private static final Application application = Application.getInstance();
    // SQL 常量
    private static final String INSERT_MOVIE_DETAIL = "INSERT INTO movie_detail (vod_id, vod_name, vod_en, vod_tag, vod_pic, vod_blurb, vod_remarks, vod_year, vod_duration, vod_time, vod_content, vod_play_url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    private static final String SELECT_MOVIE_DETAIL = "SELECT vod_id, vod_name, vod_en, vod_tag, vod_pic, vod_blurb, vod_remarks, vod_year, vod_duration, vod_time, vod_content, vod_play_url FROM movie_detail WHERE vod_id = ?";
    private static final String DELETE_MOVIE_DETAIL = "DELETE FROM movie_detail WHERE vod_id = ?";
    private static final String UPDATE_MOVIE_DETAIL = "UPDATE movie_detail SET vod_name = ?, vod_en = ?, vod_tag = ?, vod_pic = ?, vod_blurb = ?, vod_remarks = ?, vod_year = ?, vod_duration = ?, vod_time = ?, vod_content = ?, vod_play_url = ? WHERE vod_id = ?";
    private static final String SELECT_GROUPED_MOVIE_DETAIL = "SELECT vod_id, vod_name, vod_en, vod_tag, vod_pic, vod_blurb, vod_remarks, vod_year, vod_duration, vod_time, vod_content, vod_play_url FROM movie_detail LIMIT ?";
    private final Connection connection;

    public MovieDetailImpl() {
        this(application.getConnection());
    }

    public MovieDetailImpl(Connection connection) {
        this.connection = connection;
    }

    @Override
    public boolean save(MovieDetailData data) {
        try (PreparedStatement stmt = connection.prepareStatement(INSERT_MOVIE_DETAIL)) {
            setMovieDetailParams(stmt, data);
            stmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean saveAll(List<MovieDetailData> dataList) {
        try (PreparedStatement stmt = connection.prepareStatement(INSERT_MOVIE_DETAIL)) {
            for (MovieDetailData data : dataList) {
                setMovieDetailParams(stmt, data);
                stmt.addBatch();
            }
            stmt.executeBatch();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    private void setMovieDetailParams(PreparedStatement stmt, MovieDetailData data) throws SQLException {
        stmt.setInt(1, data.getVodId());
        stmt.setString(2, data.getVodName());
        stmt.setString(3, data.getVodEn());
        stmt.setString(4, data.getVodTag());
        stmt.setString(5, data.getVodPic());
        stmt.setString(6, data.getVodBlurb());
        stmt.setString(7, data.getVodRemarks());
        stmt.setString(8, data.getVodYear());
        stmt.setString(9, data.getVodDuration());
        stmt.setString(10, data.getVodTime());
        stmt.setString(11, data.getVodContent());
        stmt.setString(12, data.getVodPlayUrl());
    }

    @Override
    public MovieDetailData getMovieDetailById(int vodId) {
        try (PreparedStatement stmt = connection.prepareStatement(SELECT_MOVIE_DETAIL)) {
            stmt.setInt(1, vodId);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return mapRowToMovieDetail(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    private MovieDetailData mapRowToMovieDetail(ResultSet rs) throws SQLException {
        MovieDetailData movieDetailData = new MovieDetailData();
        movieDetailData.setVodId(rs.getInt("vod_id"));
        movieDetailData.setVodName(rs.getString("vod_name"));
        movieDetailData.setVodEn(rs.getString("vod_en"));
        movieDetailData.setVodTag(rs.getString("vod_tag"));
        movieDetailData.setVodPic(rs.getString("vod_pic"));
        movieDetailData.setVodBlurb(rs.getString("vod_blurb"));
        movieDetailData.setVodRemarks(rs.getString("vod_remarks"));
        movieDetailData.setVodYear(rs.getString("vod_year"));
        movieDetailData.setVodDuration(rs.getString("vod_duration"));
        movieDetailData.setVodTime(rs.getString("vod_time"));
        movieDetailData.setVodContent(rs.getString("vod_content"));
        movieDetailData.setVodPlayUrl(rs.getString("vod_play_url"));
        return movieDetailData;
    }

    @Override
    public boolean deleteById(int vodId) {
        try (PreparedStatement stmt = connection.prepareStatement(DELETE_MOVIE_DETAIL)) {
            stmt.setInt(1, vodId);
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(MovieDetailData data) {
        try (PreparedStatement stmt = connection.prepareStatement(UPDATE_MOVIE_DETAIL)) {
            setMovieDetailParams(stmt, data);
            stmt.setInt(13, data.getVodId());
            stmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Map<String, List<MovieDetailData>> getGroupedDataByTypeName(int limit) {
        Map<String, List<MovieDetailData>> groupedData = new HashMap<>();
        try (PreparedStatement stmt = connection.prepareStatement(SELECT_GROUPED_MOVIE_DETAIL)) {
            stmt.setInt(1, limit);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                MovieDetailData movieDetailData = mapRowToMovieDetail(rs);
                String typeName = movieDetailData.getVodTag();
                groupedData.computeIfAbsent(typeName, k -> new ArrayList<>()).add(movieDetailData);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return groupedData;
    }
}
